/*-
 * Copyright (c) 2006, 2007 FTS Team
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 4. Neither the name of the FTS Team nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 *      Allocator.hpp
 *
 * $FTS$
 */
#ifndef _ALLOCATOR_HPP__
#define _ALLOCATOR_HPP__ 1

/**
  @file Allocator.hpp
  @brief Generic memory allocator
*/

#include <Types.h>

#ifndef _OPERATOR_NEW_INSTEAD_OF_MALLOC
#include <stdlib.h>
#endif // _OPERATOR_NEW_INSTEAD_OF_MALLOC

#include <new>

namespace FTSS // FullTextSearchSystem
{

/**
  @class Allocator Allocator.hpp <Allocator.hpp>
  @brief Generic memory allocator
*/
template<typename T> class Allocator
{
public:
	/**
	  @brief Allocate some memory
	  @param iObjects -  number of objects
	  @return pointer to allocated memory
	*/
	inline T * allocate(const UINT_32 & iObjects = 1)
#ifdef _OPERATOR_NEW_INSTEAD_OF_MALLOC
	{
		return (T *) operator new(iObjects * sizeof(T));
	}
#else
	{
		return (T *) malloc(iObjects * sizeof(T));
	}
#endif // _OPERATOR_NEW_INSTEAD_OF_MALLOC

	/**
	  @brief De-Allocate some memory
	  @param pT - memory to deallocate
	*/
	inline void deallocate(T * pT)
#ifdef _OPERATOR_NEW_INSTEAD_OF_MALLOC
	{
		operator delete(pT);
	}
#else
	{
		free(pT);
	}
#endif // _OPERATOR_NEW_INSTEAD_OF_MALLOC

	/**
	  @brief Create objects in memory
	  @param pT - poiter to reserved memory
	  @param iObjects - number of objects
	*/
	inline void construct(T * pT, const UINT_32 & iObjects = 1)
	{
		// Hack for stupid gcc 3.X compiler
		for (UINT_32 iI = 0; iI < iObjects; ++iI) { ::new(&pT[iI]) T(); }
	}

	/**
	  @brief Create object from existing (execute copy construcotr)
	  @param pT - poiter to reserved memory
	  @param oRhs - object to copy
	*/
	inline void construct(T * pT, const T & oRhs) { ::new(pT) T(oRhs); }

	/**
	  @brief Destroy objects
	  @param pT - poiter to memory
	  @param iObjects - number of objects to destroy
	*/
	inline void destruct(T * pT, const UINT_32 & iObjects = 1)
	{
		for (UINT_32 iI = 0; iI < iObjects; ++iI)
		{
			pT[iI].~T();
		}
	}
};

} // namespace FTSS
#endif // _ALLOCATOR_HPP__
// End.
